package util;

import java.util.Random;
import java.util.function.Supplier;

/**
 * @Author Antony
 * @Since 2018/6/20 16:01
 */
public class BaseUtil {

    /**
     * 获取指定长度的随机整数数组
     * 只有正数
     * @param size
     * @return
     */
    public static int[] randomIntArray(int size, int bound){
        return randomIntArray(size, bound, true);
    }

    /**
     * 生成指定长度数组，随机正负
     * @param size
     * @param bound
     * @param positive
     * @return
     */
    public static int[] randomIntArray(int size, int bound, boolean positive){
        int result[] = new int[size];
        Random r = new Random(System.nanoTime());
        Supplier<Integer> s = () -> r.nextInt(bound);
        if(!positive){
            s = () -> r.nextInt(bound*2)-bound;
        }
        for(int i=0; i<size; i++){
            result[i] = s.get();
        }
        return result;
    }


    public static void main(String[] args) {
        int arr[] = BaseUtil.randomIntArray(20, 100);
        int arr2[] = BaseUtil.randomIntArray(20,100, true);
        System.out.println(GsonUtil.toJson(arr));
        System.out.println(GsonUtil.toJson(arr2));
    }
}
